﻿#define DEBUG
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Framework.Common;
using System.Drawing.Printing;
using Model.Custome;
using System.Threading;
using System.IO;
using Framework;

namespace DomPrint
{
    public partial class BaseShowForm : BaseForm
    {
        protected int printNum;
        protected List<ListItemInfo> listItem;

        public BaseShowForm()
        {
            InitializeComponent();
        }

        public BaseShowForm(int printNum, List<ListItemInfo> listItem)
        {
            InitializeComponent();
            this.printNum = printNum;
            this.listItem = listItem;
            LogHelper.DebugFormat("打印数量:{0},打印内容:{1}", printNum, JsonHelper.DataToStrJSON(listItem));
        }

        protected Bitmap GetBarCode(string text)
        {
            LogHelper.DebugFormat("获取条形码:{0}", text);
            BarCode.Code128 code = new BarCode.Code128();
            code.ValueFont = new System.Drawing.Font("宋体", 9);
            Bitmap map = code.GetCodeImage(text, BarCode.Code128.Encode.Code128A);
            return map;
        }

        protected void PrintPrepare()
        {
            printDocument1.PrintPage += new PrintPageEventHandler(Form_PrintPage);
            printDocument1.OriginAtMargins = true;
            printDocument1.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
            PageSetupDialog psd = new PageSetupDialog();
            psd.EnableMetric = true;
            psd.Document = printDocument1;
        }

        protected void DoPrint()
        {
            for (int i = 0; i < printNum; i++)
            {
                printDocument1.Print();
                Thread.Sleep(200);
            }
        }

        protected void Form_PrintPage(object sender, PrintPageEventArgs e)
        {
            //打印内容
            Bitmap _NewBitmap = new Bitmap(this.Width, this.Height);
            this.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            e.Graphics.DrawImage(_NewBitmap, -10, -20, this.Width, this.Height);
            e.HasMorePages = false;

#if DEBUG
            string fileName = Path.Combine(Application.StartupPath, "log\\image" + Guid.NewGuid().ToString() + ".bmp");
            _NewBitmap.Save(fileName);
            LogHelper.DebugFormat("打印图片:{0}", fileName);
            //printPreviewDialog1.Document = printDocument1;
            //printPreviewDialog1.Show();
#endif
        }
    }
}